package com.haierp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.sql.visitor.functions.Isnull;
import com.haierp.model.PackageScale;
import com.haierp.service.IPackageLevelService;
import com.haierp.service.IPackageScaleService;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.PackageLevelQueryVO;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.shiro.ShiroUser;
import com.wangzhixuan.commons.utils.StringUtils;

/**
 * 
 * Title: PackageScaleController.java
 * Description: 
 *
 * @author jc
 * Apr 8, 2017
 *
 */
@Service("packageScaleController")
@Controller
@RequestMapping("/packageScale")
public class PackageScaleController extends BaseController {
	
	@Autowired
	private IPackageScaleService iPackageScaleService;
	
	@Autowired
	private IPackageLevelService iPackageLevelService;
	
	@RequestMapping("/add")
	@ResponseBody
	public Object add(PackageScale packageScale) {
		JsonResult<String> result = new JsonResult<>();
		if (null == packageScale.getId()) {
			if ((StringUtils.isNotBlank(packageScale.getName())) 
					&& (StringUtils.isNotBlank(packageScale.getEnName()))) {

				//Check Name or En_Name is unique
				Integer count1 = iPackageScaleService.queryPackageScalesCount(packageScale.getName(), null);
				Integer count2 = iPackageScaleService.queryPackageScalesCount(null, packageScale.getEnName());
				if ((count1 > 0) || (count2 > 0)) {
					result.buildData("包装规格名称或英文名称不可以重复").buildIsSuccess(false);
				} else {
					result.buildIsSuccess(iPackageScaleService.insert(packageScale));
				}
			} else {
				result.buildData("包装规格名称或英文名称不可以为空").buildIsSuccess(false);
			}
		} else {
			result.buildData("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Object update(PackageScale packageScale) {
		JsonResult<String> result = new JsonResult<>();
		result.buildIsSuccess(false);
		
		if (null != packageScale.getId()) {

			if ((StringUtils.isNotBlank(packageScale.getName())) 
					&& (StringUtils.isNotBlank(packageScale.getEnName()))) {

				//Check Name or En_Name is unique
				Integer count1 = iPackageScaleService.queryPackageScalesCount(packageScale.getName(), null);
				Integer count2 = iPackageScaleService.queryPackageScalesCount(null, packageScale.getEnName());
				if ((count1 > 0) || (count2 > 0)) {
					result.buildData("包装规格名称或英文名称不可以重复").buildIsSuccess(false);
				} else {
					PackageScale oldPackageScale = iPackageScaleService.selectById(packageScale.getId());
					
					//TODO:2017-04-08,jc: Data consistency?
					Boolean bIsSuccess = false;
					if (!oldPackageScale.getEnName().equals(packageScale.getEnName())) {
						
						bIsSuccess = iPackageLevelService.updatePackageScaleByEnName(packageScale.getId(), packageScale.getEnName());
					} else {
						bIsSuccess = true;
					}
					
					if (bIsSuccess) {
						result.buildIsSuccess(iPackageScaleService.updateSelectiveById(packageScale));
					}
				}
			} else {
				result.buildData("包装规格名称或英文名称不可以为空").buildIsSuccess(false);
			}
		} else {
			result.buildData("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(PackageScale packageScale) {
		JsonResult<String> result = new JsonResult<>();

		if (null != packageScale.getId()) {
			PackageLevelQueryVO packageLevelQueryVO = new PackageLevelQueryVO();
			packageLevelQueryVO.setFirstStart(0);
			packageLevelQueryVO.setPackageId(packageScale.getId());
			
			Integer count = iPackageLevelService.queryPackageLevelsCount(packageLevelQueryVO);
			if (count > 0) {
				result.buildData("包装规格信息被使用，无法删除").buildIsSuccess(false);
			} else {
				result.buildIsSuccess(iPackageScaleService.deleteSelective(packageScale));
			}
		} else {
			result.buildData("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public Object query(Long id) {
		JsonResult<PackageScale> result = new JsonResult<>();

		return result.buildData(iPackageScaleService.selectById(id)).buildIsSuccess(true);
	}
	
	@RequestMapping("/queryPackageScaleList")
	@ResponseBody
	public Object queryPackageScaleList() {
		JsonPageResult<List<PackageScale>> result = iPackageScaleService.queryPackageScaleList();
		
		return result.buildIsSuccess(true);
	}
}
